<?php

namespace app\shopapi\controller;

use app\common\controller\Api;
use app\common\controller\Shopapi;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\Area;
use app\common\model\Version;
use app\shopapi\model\lakala\ShopInfeed;
use app\shopapi\model\Shop;
use app\shopapi\model\ShopBillsStat;
use fast\Random;
use think\captcha\Captcha;
use think\Config;
use think\Hook;
use think\Log;

/**
 * 公共接口
 */
class Common extends Shopapi
{
    protected $noNeedLogin = ['init', 'captcha','upload','task'];
    protected $noNeedRight = '*';

    public function _initialize()
    {

        if (isset($_SERVER['HTTP_ORIGIN'])) {
            header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
        }
        //跨域检测
        check_cors_request();

        if (!isset($_COOKIE['PHPSESSID'])) {
            Config::set('session.id', $this->request->server("HTTP_SID"));
        }
        parent::_initialize();
    }

    /**
     * 上传文件
     * @ApiMethod (POST)
     * @param File $file 文件流
     */
    public function upload()
    {
        Config::set('default_return_type', 'json');
        //必须设定cdnurl为空,否则cdnurl函数计算错误
        Config::set('upload.cdnurl', '');
        $chunkid = $this->request->post("chunkid");
        if ($chunkid) {
            if (!Config::get('upload.chunking')) {
                $this->error(__('Chunk file disabled'));
            }
            $action = $this->request->post("action");
            $chunkindex = $this->request->post("chunkindex/d");
            $chunkcount = $this->request->post("chunkcount/d");
            $filename = $this->request->post("filename");
            $method = $this->request->method(true);
            if ($action == 'merge') {
                $attachment = null;
                //合并分片文件
                try {
                    $upload = new Upload();
                    $attachment = $upload->merge($chunkid, $chunkcount, $filename);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
            } elseif ($method == 'clean') {
                //删除冗余的分片文件
                try {
                    $upload = new Upload();
                    $upload->clean($chunkid);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            } else {
                //上传分片文件
                //默认普通上传文件
                $file = $this->request->file('file');
                try {
                    $upload = new Upload($file);
                    $upload->chunk($chunkid, $chunkindex, $chunkcount);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
        } else {
            $attachment = null;
            //默认普通上传文件
            $file = $this->request->file('file');

            try {
                $upload = new Upload($file);
                $attachment = $upload->upload();
            } catch (UploadException $e) {
                $this->error($e->getMessage());
            }

            $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
        }

    }

    public function task()
    {
        //创建订单统计
        ShopBillsStat::add();

        //查询商户申请进件状态
        $shop_list = Shop::alias('s')
            //->join('user u','s.user_id = u.id')
            ->where('s.audit_status','not in','3,4')
            ->where('s.out_member_no','<>','')
            ->field('s.out_member_no,s.shop_id')
            ->select();

        $auth_model = new ShopInfeed();

        foreach ($shop_list as $k=>$v){
            $list = $auth_model->memberMerchantInfo($v['shop_id']);

            if ($list){
                foreach ($list['merchant_list'] as $kk=>$vv){
                    $shop_detail = Shop::get($v['shop_id']);

                    //$shop_detail = Shop::get($v['shop_id']);
                    $update_data = [
                        'merchant_no'=>$vv['merchant_no'],
                        'member_no'=>$vv['member_no'],
                        'audit_status'=>$vv['merchant_status'],
                        //'bank_name'=>$bank_info['bank_name'],
                        //'bank_num'=>$bank_info['card_no'],
                        //'bank'=>$bank_info['bank_contact_name'],
                        //'bank_code'=>$bank_info['bank_contact_no'],
                        //'bank_card_id'=>$bank_info['card_id'],
                    ];

                    $bank_card_list = $auth_model->cardList($v['shop_id']);
                    if ($bank_card_list){
                        $bank_card_list = $bank_card_list['bank_card_list'];
                        $bank_info = [];
                        foreach ($bank_card_list as $k=>$v){
                            if ($v['is_settle_card']){
                                $bank_info = $v;
                            }
                        }
                        $update_data['bank_name'] = $bank_info['bank_name'];
                        $update_data['bank_num'] = $bank_info['card_no'];
                        $update_data['bank'] = $bank_info['bank_contact_name'];
                        $update_data['bank_code'] = $bank_info['bank_contact_no'];
                        $update_data['bank_card_id'] = $bank_info['card_id'];
                    }
                    //$wx_sub_merchant = $auth_model->searchMerchant($v['shop_id']);
                    //$update_data['wechat_channel_sub_merchant_no'] = $wx_sub_merchant['channel_sub_merchant_no'];
                    //$update_data = array_merge($update_data,$new_update_data);
                    //Shop::update($update_data,['shop_id'=>$v['shop_id']]);
                    $shop_detail->save($update_data);
                }
            }


        }


        $pass_shop_list = Shop::alias('s')
            //->join('user u','s.user_id = u.id')
            ->where('s.audit_status',3)
            //->where('s.shop_id',53)
            ->field('s.out_member_no,s.shop_id,s.member_no,s.wechat_channel_sub_merchant_no,s.channel_merchant_no,
            s.checkstand_merchant_no,s.is_unbind,s.channel_terminal_no')
            ->select();
        //dump($pass_shop_list);die;
        foreach ($pass_shop_list as $k=>$v){
            $shop_detail = Shop::get($v['shop_id']);
            $update_data = [];
            if (empty($v['wechat_channel_sub_merchant_no'])){
                $wx_sub_merchant = $auth_model->searchMerchant($v['shop_id']);
                $update_data['wechat_channel_sub_merchant_no'] = $wx_sub_merchant['channel_sub_merchant_no'];
            }
            /*if (empty($v['checkstand_merchant_no'])){
                $update_data['checkstand_merchant_no'] = 1;
                $auth_model->addTerminal($v['member_no'],$v['channel_merchant_no']);
            }
            if ($v['is_unbind']==0){
                $update_data['is_unbind'] = 1;
                $auth_model->merchantUnbind($v['member_no'],$v['channel_merchant_no'],$v['channel_terminal_no']);
            }*/
            $shop_detail->save($update_data);
        }

        $this->success('');
    }
}